import { defHttp } from '/@/utils/http/axios';
import {
  DataSourceListGetResultModel,
  DataSourceModel,
  DataSourcePageListGetResultModel,
  DataSourceParams,
  DataSourcePageSearchParams,
  DataSourceListSearchParams,
  UpdateDataSourceParams,
  DataSourceIDType,
} from '/@/api/gen/model/DataSourceModel';
import { UploadFileParams } from '/#/axios';
import { useGlobSetting } from '/@/hooks/setting';
import { UploadApiResult } from '../sys/model/uploadModel';

enum Api {
  // 字典管理接口
  PublicApi = '/gen/data-source',
  ListApi = `/gen/data-source/list`,
  PageApi = `/gen/data-source/page`,
  BatchApi = `/gen/data-source/batch`,
  ExportApi = `/gen/data-source/export`,
  ImportApi = `/gen/data-source/import`,
}
/**
 * @description:  获取数据源列表
 * @param params
 * @returns
 */
export const getDataSourceList = (params?: DataSourceListSearchParams) =>
  defHttp.get<DataSourceListGetResultModel>({ url: Api.ListApi, params });

/**
 * @description:  分页获取数据源列表
 * @param params
 * @returns
 */
export const getDataSourcePage = (params: DataSourcePageSearchParams) =>
  defHttp.get<DataSourcePageListGetResultModel>({ url: Api.PageApi, params });

/**
 * @description:  根据id获取数据源
 * @param id
 * @returns
 */
export const getDataSource = (id: DataSourceIDType) =>
  defHttp.get<DataSourceModel>({ url: `${Api.PublicApi}/${id}` });

/**
 * @description:  创建数据源
 * @param params
 * @returns
 */
export const createDataSource = (params: DataSourceParams) =>
  defHttp.post<boolean>({ url: Api.PublicApi, params });

/**
 * @description:  更新数据源
 * @param params
 * @returns
 */
export const updateDataSource = (params: UpdateDataSourceParams) =>
  defHttp.put<boolean>({ url: Api.PublicApi, params });

/**
 * @description:  物理删除数据源
 * @param id
 * @returns
 */
export const deleteDataSource = (id: DataSourceIDType) =>
  defHttp.delete<boolean>({ url: `${Api.PublicApi}/${id}` });

/**
 * @description:  物理批量删除数据源
 * @param ids
 * @returns
 */
export const deleteDataSourceBatch = (ids: DataSourceIDType[]) =>
  defHttp.post<boolean>({ url: Api.BatchApi, params: { ids } });

/**
 * @description:  逻辑删除数据源
 * @param ids
 * @returns
 */
export const logicDeleteDataSource = (id: DataSourceIDType) =>
  defHttp.delete<boolean>({ url: `${Api.PublicApi}/${id}` });

/**
 * @description:  逻辑批量删除数据源
 * @param ids
 * @returns
 */
export const logicDeleteDataSourceBatch = (ids: DataSourceIDType[]) =>
  defHttp.post<boolean>({ url: Api.BatchApi, params: { ids } });

/**
 * @description:  导出文件
 * @param ids
 * @returns
 */
export const exportDataSourceListFile = (ids: DataSourceIDType[]) =>
  defHttp.post<boolean>({ url: Api.ExportApi, params: { ids } });

/**
 * @description:  导入文件
 * @param ids
 * @returns
 */
export const importDataSourceListFile = (
  params: UploadFileParams,
  onUploadProgress?: (progressEvent: ProgressEvent) => void,
) =>
  defHttp.uploadFile<UploadApiResult>(
    {
      url: useGlobSetting().apiUrl + Api.ImportApi,
      onUploadProgress,
    },
    params,
  );
